package com.wemei.auto.ui.widget.jsbridge;


import android.text.TextUtils;
import android.webkit.JsPromptResult;
import android.webkit.WebView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wemei.auto.ui.utils.Logger;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author Administrator on 2017/3/1 0001.
 */

public class InjectedUtils {

    public static void handH5(HashMap<String, BaseInjected> injectedObjectMap,
                              WebView webView,
                              String url,
                              String message,
                              String defaultValue,
                              JsPromptResult result) {
        if (!TextUtils.isEmpty(message)) {
            try {
                com.wemei.auto.ui.widget.jsbridge.InjectedBean injectedBean = JSON.parseObject(message, InjectedBean.class);
                String apiName = injectedBean.getApiName();
                int lastIndexOf = apiName.lastIndexOf(".");
                String api = "";
                String methodName = apiName;
                if (lastIndexOf != -1) {
                    api = apiName.substring(0, lastIndexOf);
                    methodName = apiName.substring(lastIndexOf + 1);
                }
                Object injectedObject = injectedObjectMap.get(api);
                String param = injectedBean.getParam();
                JSONObject paramJsonObject = JSON.parseObject(param);
//                Logger.e("api==>" + api + "<==methodName==>" + methodName + "<==paramJsonObject==>" + paramJsonObject.toString());
//                Logger.e("callbacks==>" + injectedBean.getCallbacks());
                Object value = InjectedUtils.invoke(webView, injectedObject, methodName, paramJsonObject, injectedBean.getCallbacks());
//                Logger.e("value==>" + value);
                result.confirm(value == null ? "" : value.toString());
            } catch (Exception e) {
                Logger.e(e);
            }
        }
    }

    public static Object invoke(WebView webView, Object object, String methodName, JSONObject paramJsonObject, String callbacks) {
        if (object != null && !TextUtils.isEmpty(methodName)) {
            try {
                ArrayList<Class> paramClass = new ArrayList<>();
                ArrayList<Object> paramObject = new ArrayList<>();
                if (!paramJsonObject.isEmpty()) {
                    paramClass.add(JSONObject.class);
                    paramObject.add(paramJsonObject);
                }
                if (!TextUtils.isEmpty(callbacks)) {
                    String[] callbacksArr = callbacks.split(",");
                    for (String callback : callbacksArr) {
                        paramClass.add(com.wemei.auto.ui.widget.jsbridge.JsCallback.class);
                        paramObject.add(new JsCallback(callback, webView));
                    }
                }
                Method declaredMethod = object.getClass().getDeclaredMethod(methodName, paramClass.toArray(new Class[]{}));
                return declaredMethod.invoke(object, paramObject.toArray());
            } catch (Exception e) {
                Logger.e(e);
            }
        }
        return null;
    }
}
